package com.cory.boot.domain.bean;

import lombok.Data;

@Data
public class Result<T> implements java.io.Serializable {

    private static final long serialVersionUID = 5459992934994412878L;
    
    public static final int OK = 0;
    public static final int FAIL = -1;
    public static final String OK_MSG = "ok";
    public static final String FAIL_MSG = "fail";

    private int code;
    private String msg;
    private T data;
    
    public Result<T> data(T data) {
        this.data = data;
        return this;
    }
    
    public static <T> Result<T> custom(int code, String msg, T data) {
        Result<T> result = new Result<>();
        result.setCode(code);
        result.setMsg(msg);
        result.setData(data);
        return result;
    }
    
    
    public static Result<Object> custom(int code, String msg) {
        Result<Object> result = new Result<>();
        result.setCode(code);
        result.setMsg(msg);
        return result;
    }

    public static <T> Result<T> success(String msg, T data) {
        Result<T> result = new Result<>();
        result.setCode(OK);
        result.setMsg(msg == null ? OK_MSG : msg);
        result.setData(data);
        return result;
    }
    

    public static Result<Object> success(String msg) {
        Result<Object> result = new Result<>();
        result.setCode(OK);
        result.setMsg(msg == null ? OK_MSG : msg);
        return result;
    }
    
    public static Result<Object> success() {
        Result<Object> result = new Result<>();
        result.setCode(OK);
        result.setMsg(OK_MSG);
        return result;
    }

    public static <T> Result<T> fail(String msg, T data) {
        Result<T> result = new Result<>();
        result.setCode(FAIL);
        result.setMsg(msg == null ? FAIL_MSG : msg);
        result.setData(data);
        return result;
    }

    public static Result<Object> fail(String msg) {
        Result<Object> result = new Result<>();
        result.setCode(FAIL);
        result.setMsg(msg == null ? FAIL_MSG : msg);
        return result;
    }

    public static Result<Throwable> error(Throwable e) {
        Result<Throwable> result = new Result<>();
        result.setCode(FAIL);
        result.setMsg("服务器繁忙，请稍候再试");
        result.setData(e);
        return result;
    }
}